const mysql = require('mysql')
const {
    MYSQL_CONFIG
} = require('../config')
//创建连接
let connection = mysql.createConnection(MYSQL_CONFIG)


//开始连接
connection.connect();


function execSQL(sql) {
    try {
      return new Promise((resolve, reject) => {
        connection.query(sql, (e, r, f) => {
            if (e) {
                reject(e);
                return;
            }
                resolve(r)
        })
    })
    } catch (e) {
        connection.destroy()

        connection =  mysql.createConnection(MYSQL_CONFIG);
        connection.connect();
        
        return new Promise((resolve, reject) => {
        connection.query(sql, (e, r, f) => {
            if (e) {
                reject(e);
                return;
            }
                resolve(r)
        })
    })
    }
    
}

module.exports = {
    execSQL
}